﻿# 生成协议脚本

$current_path = Resolve-Path "."
$protoc_path= Join-Path $PSScriptRoot "../bin/protoc.exe"
$ts_plugin_path= Join-Path $PSScriptRoot "../bin/node_modules/.bin/protoc-gen-ts_proto.cmd"
$proto_universal_path= Join-Path $PSScriptRoot "universal/"
$proto_client_only_path= Join-Path $PSScriptRoot "client_only/"
$proto_server_only_path= Join-Path $PSScriptRoot "server_only/"
$unity_universal_path= Join-Path $PSScriptRoot "../../Assets/Scripts/Protos/Universal/"
$unity_client_only_path= Join-Path $PSScriptRoot "../../Assets/Scripts/Protos/ClientOnly/"
$account_universal_path= Join-Path $PSScriptRoot "../../Server/Account/protos/universal/"
$account_server_only_path= Join-Path $PSScriptRoot "../../Server/Account/protos/server_only/"
# $lobby_universal_path= Join-Path $PSScriptRoot "../../Server/Lobby/protos/universal/"
# $lobby_server_only_path= Join-Path $PSScriptRoot "../../Server/Lobby/protos/server_only/"
# $game_universal_path= Join-Path $PSScriptRoot "../../Server/Game/protos/universal/"
# $game_server_only_path= Join-Path $PSScriptRoot "../../Server/Game/protos/server_only/"

# wprotobuf无法自动退出，模拟用户输入回车键以退出
$wshell = New-Object -ComObject WScript.Shell
$wshell.SendKeys("{ENTER}")
$wshell.SendKeys("{ENTER}")
$wshell.SendKeys("{ENTER}")
$wshell.SendKeys("{ENTER}")

######
# universal 协议
######
Set-Location $proto_universal_path
try {
    Write-Host "[universal] 开始生成协议..."
    $protoFiles = Get-ChildItem -Recurse -Filter *.proto
    if (Test-Path $unity_universal_path) {
        # $null = Remove-Item -Recurse -Force -Path $unity_universal_path
        # 清除 $unity_universal_path 目录下所有cs文件，而不处理其他文件
        Get-ChildItem -Path $unity_universal_path -Filter *.cs -Recurse | Remove-Item -Force
    }
    $null = New-Item -ItemType Directory -Force -Path $unity_universal_path
    if (Test-Path $account_universal_path) {
        $null = Remove-Item -Recurse -Force -Path $account_universal_path
    }
    $null = New-Item -ItemType Directory -Force -Path $account_universal_path
    # if (Test-Path $lobby_universal_path) {
    #     $null = Remove-Item -Recurse -Force -Path $lobby_universal_path
    # }
    # $null = New-Item -ItemType Directory -Force -Path $lobby_universal_path
    # if (Test-Path $game_universal_path) {
    #     $null = Remove-Item -Recurse -Force -Path $game_universal_path
    # }
    # $null = New-Item -ItemType Directory -Force -Path $game_universal_path
    if ($protoFiles.Count -eq 0) {
        Write-Error "[universal] 没有找到任何 proto 文件"
    }
    else {
        & $protoc_path `
            -o ProtoDescriptor.bp `
            -I . `
            -I ../../bin/protocols `
            -I ../../bin/protocols/extensions `
            -I ../../bin/protocols/extensions/v3 `
            --csharp_wxb_out=$unity_universal_path `
            ../../bin/protocols/pb_header_v3.proto `
            $protoFiles
        if ($LASTEXITCODE -ne 0) {
            Write-Error "[universal] client 生成失败"
            Set-Location $current_path
            exit 1
        }
        Write-Host "[universal] client 生成完成"
        & $protoc_path `
            --plugin=protoc-gen-ts_proto=$ts_plugin_path `
            -I . `
            -I ../../bin/protocols `
            -I ../../bin/protocols/extensions `
            -I ../../bin/protocols/extensions/v3 `
            --ts_proto_out=$account_universal_path `
            --ts_proto_opt=esModuleInterop=true,outputClientImpl=false,unrecognizedEnum=false `
            ../../bin/protocols/pb_header_v3.proto `
            $protoFiles
        if ($LASTEXITCODE -ne 0) {
            Write-Error "[universal] account 生成失败"
            Set-Location $current_path
            exit 1
        }
        Write-Host "[universal] account 生成成功"
    }
}
catch {
    Write-Error "[universal] 生成失败"
    Set-Location $current_path
    exit 1
}
Set-Location $current_path

######
# client only 协议
######
Set-Location $proto_client_only_path
try {
    Write-Host "`n[client only] 开始生成协议..."
    $protoFiles = Get-ChildItem -Recurse -Filter *.proto
    if (Test-Path $unity_client_only_path) {
        # $null = Remove-Item -Recurse -Force -Path $unity_client_only_path
        # 清除 $unity_client_only_path 目录下所有cs文件，而不处理其他文件
        Get-ChildItem -Path $unity_client_only_path -Filter *.cs -Recurse | Remove-Item -Force
    }
    $null = New-Item -ItemType Directory -Force -Path $unity_client_only_path
    if ($protoFiles.Count -eq 0) {
        Write-Error "[client only] 没有找到任何 proto 文件"
    }
    else {
        & $protoc_path `
            -o ProtoDescriptor.bp `
            -I . `
            -I ../../bin/protocols `
            -I ../../bin/protocols/extensions `
            -I ../../bin/protocols/extensions/v3 `
            --csharp_wxb_out=$unity_client_only_path `
            $protoFiles
        if ($LASTEXITCODE -ne 0) {
            Write-Error "[client only] 生成失败"
            Set-Location $current_path
            exit 1
        }
        Write-Host "[client only] 生成成功"
    }
}
catch {
    Write-Error "[client only] 生成失败"
    Set-Location $current_path
    exit 1
}
Set-Location $current_path

######
# server only 协议
######
Set-Location $proto_server_only_path
try {
    Write-Host "`n[server only] 开始生成协议..."
    $protoFiles = Get-ChildItem -Recurse -Filter *.proto
    if (Test-Path $account_server_only_path) {
        $null = Remove-Item -Recurse -Force -Path $account_server_only_path
    }
    $null = New-Item -ItemType Directory -Force -Path $account_server_only_path
    # if (Test-Path $lobby_universal_path) {
    #     $null = Remove-Item -Recurse -Force -Path $lobby_universal_path
    # }
    # $null = New-Item -ItemType Directory -Force -Path $lobby_universal_path
    # if (Test-Path $game_universal_path) {
    #     $null = Remove-Item -Recurse -Force -Path $game_universal_path
    # }
    # $null = New-Item -ItemType Directory -Force -Path $game_universal_path
    if ($protoFiles.Count -eq 0) {
        Write-Error "[server only] 没有找到任何 proto 文件"
    }
    else {
        & $protoc_path `
            --plugin=protoc-gen-ts_proto=$ts_plugin_path `
            -o ProtoDescriptor.bp `
            -I . `
            -I ../../bin/protocols `
            -I ../../bin/protocols/extensions `
            -I ../../bin/protocols/extensions/v3 `
            --ts_proto_out=$account_server_only_path `
            --ts_proto_opt=esModuleInterop=true,outputClientImpl=false,unrecognizedEnum=false `
            $protoFiles
        if ($LASTEXITCODE -ne 0) {
            Write-Error "[server only] account 生成失败"
            Set-Location $current_path
            exit 1
        }
        Write-Host "[server only] account 生成成功"
    }
}
catch {
    Write-Error "[server only] 生成失败"
    Set-Location $current_path
    exit 1
}
Set-Location $current_path